package com.dc.controller;

import com.dc.baseController.BaseController;
import com.dc.em.ErrorMessageEnum;
import com.dc.model.Shopcar;
import com.dc.model.User;
import com.dc.service.ShopcarService;
import com.dc.vo.BaseModel;
import com.dc.vo.BusinessException;
import com.wordnik.swagger.annotations.Api;
import com.wordnik.swagger.annotations.ApiImplicitParam;
import com.wordnik.swagger.annotations.ApiImplicitParams;
import com.wordnik.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

@Controller
@Api("购物车模块")
@RequestMapping("/Shopcar")
public class ShopcarController extends BaseController {
    @Autowired
    ShopcarService shopcarService;
    @RequestMapping(value = "/selectPageShopcarById/{id}",method = RequestMethod.POST)
    @ApiOperation(value = "按用户id查询购物车",httpMethod = "POST")
    @ResponseBody
    @ApiImplicitParams({
            @ApiImplicitParam(value = "当前页面",name = "curr_page",defaultValue = "1",dataType = "Integer",paramType = "query"),
            @ApiImplicitParam(value = "一页的最大条数",name = "page_size",defaultValue = "10",dataType = "Integer",paramType = "query")
    })
    public BaseModel selectPageShopcarById (@PathVariable(value = "id") Integer id, Integer curr_page, Integer page_size, BaseModel baseModel, HttpServletRequest request) throws Exception{
        HttpSession session = request.getSession();
        User user = (User) session.getAttribute("currentUser");
        if(user == null){
            throw new BusinessException(ErrorMessageEnum.LOGIN_TIMEOUT);
        }
        return shopcarService.selectPageShopcarById(id,curr_page,page_size,baseModel);
    }
    @RequestMapping(value = "/addShopcar",method = RequestMethod.POST)
    @ApiOperation(value = "加入购物车",httpMethod = "POST",notes = "新增")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "用户id",name = "user_id",required = true,dataType = "Integer",paramType = "query"),
            @ApiImplicitParam(value = "商品id",name = "shoes_id",required = true,dataType = "Integer",paramType = "query"),
            @ApiImplicitParam(value = "商品数量",name = "shoes_num",required = true,dataType = "Integer",paramType = "query")
    })
    @ResponseBody
    public BaseModel addShoes(Shopcar shopcar, BaseModel baseModel, HttpServletRequest request) throws Exception{
        HttpSession session = request.getSession();
        User user = (User) session.getAttribute("currentUser");
        if(user == null){
            throw new BusinessException(ErrorMessageEnum.LOGIN_TIMEOUT);
        }
        shopcar.setIs_deleted(0);
        shopcar.setVersion(0);
        return shopcarService.addShopcar(shopcar,baseModel);
    }
    @RequestMapping(value = "/deletedShopcar/{ids}",method = RequestMethod.POST)
    @ApiOperation(value="移除购物车",httpMethod = "POST",notes = "删除")
    @ResponseBody
    public BaseModel deletedShopcar(@PathVariable("ids") String ids, BaseModel baseModel, HttpServletRequest request) throws Exception{
        HttpSession session = request.getSession();
        User user = (User) session.getAttribute("currentUser");
        if(user == null){
            throw new BusinessException(ErrorMessageEnum.LOGIN_TIMEOUT);
        }
        return shopcarService.deletedShopcar(ids,baseModel);
    }
}
